package com.zelin.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zelin.page.entity.GoodsDescEntity;
import com.zelin.page.entity.GoodsEntity;
import com.zelin.page.entity.ItemEntity;
import com.zelin.page.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/4-15:42
 * ------------------------------
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;
    //1. 根据商品id生成静态页面
    @Override
    public void createHtml(Long id) {
        try {
            //1.1 定义模板工作的上下文环境
            Context context = new Context();

            //1.2 定义最终生成的html页面的位置
            File file = new File("D:\\nginx-1.8.0\\html\\item\\"+ id + ".html");
            Writer writer = new FileWriter(file );

            //1.3 定义模板中需要存放数据的集合
            Map<String, Object> dataMap = new HashMap<>();

            //1.4 查询spu商品
            GoodsEntity goods = goodsService.getById(id);
            System.out.println("goods = " + goods);

            //1.5 查询商品描述表的数据
            GoodsDescEntity goodsDesc = goodsDescService.getById(id);

            //1.6 查询sku商品列表
            List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id).orderByDesc("is_default"));

            //1.5 将spu商品放到datamap中
            dataMap.put("goods",goods);
            dataMap.put("goodsDesc",goodsDesc);

            //1.7 将sku商品列表放到dataMap中
            dataMap.put("items",items);
            dataMap.put("sku",items.get(0));

            //1.6 查询各个spu商品的属性
            List<Map> customAttributeItems = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
            List<Map> specificationItems = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
            List<Map> itemImages = JSON.parseArray(goodsDesc.getItemImages(), Map.class);

            //1.7 查询分类
            String name1 = itemCatService.getById(goods.getCategory1Id()).getName();
            String name2 = itemCatService.getById(goods.getCategory2Id()).getName();
            String name3 = itemCatService.getById(goods.getCategory3Id()).getName();

            dataMap.put("customAttributeItems", customAttributeItems);
            dataMap.put("specificationItems",specificationItems);
            dataMap.put("itemImages",itemImages);
            dataMap.put("name1",name1);
            dataMap.put("name2",name2);
            dataMap.put("name3",name3);
            context.setVariables(dataMap);

            templateEngine.process("item",context,writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
